<?php
namespace app\route;
use think\Route;
use think\Config;
use think\DB;
use think\Request;

$request = Request::instance();
/**
 * 前台根据域名识别到id_org，后台管理根据登录帐号识别id_org
 * @author JohnnyDen <413819942@qq.com>
 */

$userInfo   = \think\Session::get('userInfo');
if ($userInfo) {
    config('orgId', (int)$userInfo['orgId']);
    config('user_name',$userInfo['username']);
} else {
    $id_org = db('Domains')->where('domain', $request->host())->value('id_org');
    config('orgId', (int)$id_org);
}

$settings = db('Settings')->where('id_org', config('orgId'))->select();
foreach ($settings as $k => $v) {
    $mipInfo[$v['key']] = $v['val'];
}
$tplName = $mipInfo['template'];
$rewrite = $mipInfo['rewrite'] ? '' : '/index.php?s=';
$domain = $request->domain() . $rewrite;
$domainStatic = str_replace('/index.php?s=', '', $domain);$domains = $domainStatic . '/?s=';
if ($mipInfo['superSites']) {
    $domainSitesList = db('domainSites')->where('id_org', config('orgId'))->select();
    $domainSettingsInfo = null;
    if ($domainSitesList) {
        $siteInfo = db('domainSites')->where('id_org', config('orgId'))->where('domain',$request->server()['HTTP_HOST'])->find();
        if ($siteInfo) {
            $domain = $siteInfo['http_type'] . $siteInfo['domain'] . $rewrite;                        $domains = $siteInfo['http_type'] . $siteInfo['domain'] . '/?s=';
            $domainStatic = $siteInfo['http_type'] . $siteInfo['domain'];
            $tplName = $siteInfo['template'];
            config('dataId',$siteInfo['id']);
            $domainSettingsInfo = db('domainSettings')->where('id',$siteInfo['id'])->find();
            config('domainSettingsInfo',$domainSettingsInfo);
        } else {
            $domain = $request->domain() . $rewrite;            
            $domainStatic = str_replace('/index.php?s=', '', $domain);                        $domains = $domainStatic . '/?s=';
        }
    }
} else {
    $domain = $request->domain() . $rewrite;
    $domainStatic = str_replace('/index.php?s=', '', $domain);        $domains = $domainStatic . '/?s=';
    if ($mipInfo['domain']) {
        $domain = $mipInfo['httpType'] . $mipInfo['domain'] . $rewrite;                $domains = $mipInfo['httpType'] . $mipInfo['domain'] . '/?s=';
        $domainStatic = $mipInfo['httpType'] . $mipInfo['domain'];
    }
    if ($mipInfo['articleDomain']) {		$rewrite = $mipInfo['rewrite'] ? '' : '/index.php?s=';		
        $domain =  $mipInfo['articleDomain'];		if (strpos($domain{(strlen(trim($domain))-1)},'/') !== false) {           $domain = substr($domain,0,strlen($domain)-1);         }                $domain = $domain . $rewrite;      	$domainStatic = str_replace('/index.php?s=', '', $domain);                $domains = $domainStatic . '/?s=';
    }
}
config('domain',$domain);config('domains',$domains);
config('domainStatic',$domainStatic);
config('mipauthorization',false);
config('mipInfo',$mipInfo);
if (Request::instance()->isMobile()) {
    if ($mipInfo['superTpl']) {
        $tplName = $tplName; //默认'm';
    }
}
$tplName = $tplName ? $tplName : 'default';
config('view_name',$tplName);$pages = [];$template = config('template.view_path') . $tplName . DS;if (is_dir($template)) {    $templateFile = opendir($template);    if ($templateFile) {        while (false !== ($file = readdir($templateFile))) {            if (substr($file, 0, 1) != '.' AND is_dir($template . DS . $file)) {                $pages[] = $file;            }        }        closedir($templateFile);    }}$subHtml = '';if ($pages) {	foreach ($pages as $key => $value) {		if (strpos($value, 'html') !== false) {			$subHtml = $value . DS;		}
	}}

config('template.view_path',config('template.view_path') . $tplName . DS . $subHtml);
config('admin','admin'); //如果修改系统管理地址，请修改后一个admin即可
config('routeStatus',true);

foreach (fetch_file_lists(ROOT_PATH . 'addons' . DS) as $key => $file) {
    if (strstr($file,'route.php')) {
        require $file;
    }
}


if (!strpos($request->url(),'Api')) {
    if (config('routeStatus')) {
        Route::rule('/sitemap.xml','index/Index/sitemap');
        Route::rule(['xml/:id' => ['index/Index/xml?id=:id',['ext'=>'xml'],['id'=>'\d+']]]);
        Route::rule(['tagXml/:id' => ['index/Index/tagXml?id=:id',['ext'=>'xml'],['id'=>'\d+']]]);
        Route::rule('/baiduSitemapPc.xml','index/Index/baiduSitemapPc');
        Route::rule(['pcXml/:id' => ['index/Index/pcXml?id=:id',['ext'=>'xml'],['id'=>'\d+']]]);
        Route::rule('/','index/Index/index');
        Route::group($mipInfo['tagModelUrl'], [
            '[:id]/index'.$mipInfo['urlPageBreak'].'<page>'=>  ['tag/Tag/tagDetail',['ext'=>'html'],['id'=>'^[a-zA-Z0-9_-]+'],['page'=>'\d+']],
            ':id'=>  ['tag/Tag/tagDetail',[],['id'=>'^[a-zA-Z0-9_-]+']],
        ],[],[]);
    }
    Route::group(Config::get('admin'), [
        ':model/:action/:addonsCtr/:addonsAct/[:params]' => 'admin/Admin/index?model=:model&action=:action&params=:params&addonsAct=:addonsAct&addonsCtr=:addonsCtr',
        ':model/:action/:params' => 'admin/Admin/index?model=:model&action=:action&params=:params',
        ':model/:action/' => 'admin/Admin/index?model=:model&?action=:action',
        ':model' => 'admin/Admin/index?model=:model',
        '/' => 'admin/Admin/index',
    ],[],[]);
} else {    Route::rule(Config::get('admin').'/:ctr/:act','admin/:ctr/:act');}
if (config('routeStatus')) {    foreach (fetch_file_lists(APP_PATH) as $key => $file) {        if (strstr($file,'route.php')) {            require_once $file;        }    }}